home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / tools / bison.lha / bison++-1.04 / LR0.c < prev    next >
C/C++ Source or Header  |  1989-06-09  |  14KB  |  668 lines

  1. /* Generate the nondeterministic finite state machine for bison,
  2.    Copyright (C) 1984, 1986, 1989 Free Software Foundation, Inc.
  3.  
  4. This file is part of Bison, the GNU Compiler Compiler.
  5.  
  6. Bison is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. Bison is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with Bison; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* See comments in state.h for the data structures that represent it.
  22.    The entry point is generate_states.  */
  23.  
  24. #include <stdio.h>
  25. #include "machine.h"
  26. #include "new.h"
  27. #include "gram.h"
  28. #include "state.h"
  29.  
  30.  
  31. extern char *nullable;
  32. extern short *itemset;
  33. extern short *itemsetend;
  34.  
  35.  
  36. int nstates;
  37. int final_state;
  38. core *first_state;
  39. shifts *first_shift;
  40. reductions *first_reduction;
  41.  
  42. int get_state();
  43. core *new_state();
  44.  
  45. void new_itemsets();
  46. void append_states();
  47. void initialize_states();
  48. void save_shifts();
  49. void save_reductions();
  50. void augment_automaton();
  51. void insert_start_shift();
  52. extern void initialize_closure();
  53. extern void closure();
  54. extern void finalize_closure();
  55. extern void toomany();
  56.  
  57. static core *this_state;
  58. static core *last_state;
  59. static shifts *last_shift;
  60. static reductions *last_reduction;
  61.  
  62. static int nshifts;
  63. static short *shift_symbol;
  64.  
  65. static short *redset;
  66. static short *shiftset;
  67.  
  68. static short **kernel_base;
  69. static short **kernel_end;
  70. static short *kernel_items;
  71.  
  72. /* hash table for states, to recognize equivalent ones.  */
  73.  
  74. #define    STATE_TABLE_SIZE    1009
  75. static core **state_table;
  76.  
  77.  
  78.  
  79. void
  80. allocate_itemsets()
  81. {
  82.   register short *itemp;
  83.   register int symbol;
  84.   register int i;
  85.   register int count;
  86.   register int max;
  87.   register short *symbol_count;
  88.  
  89.   count = 0;
  90.   symbol_count = NEW2(nsyms, short);
  91.  
  92.   itemp = ritem;
  93.   symbol = *itemp++;
  94.   while (symbol)
  95.     {
  96.       if (symbol > 0)
  97.     {
  98.       count++;
  99.       symbol_count[symbol]++;
  100.     }
  101.       symbol = *itemp++;
  102.     }
  103.  
  104.   /* see comments before new-itemset.  All the vectors of items
  105.      live inside kernel_items.  The number of active items after
  106.      some symbol cannot be more than the number of times that symbol
  107.      appears as an item, which is symbol_count[symbol].
  108.      We allocate that much space for each symbol.  */
  109.  
  110.   kernel_base = NEW2(nsyms, short *);
  111.   kernel_items = NEW2(count, short);
  112.  
  113.   count = 0;
  114.   max = 0;
  115.   for (i = 0; i < nsyms; i++)
  116.     {
  117.       kernel_base[i] = kernel_items + count;
  118.       count += symbol_count[i];
  119.       if (max < symbol_count[i])
  120.     max = symbol_count[i];
  121.     }
  122.  
  123.   shift_symbol = symbol_count;
  124.   kernel_end = NEW2(nsyms, short *);
  125. }
  126.  
  127.  
  128. void
  129. allocate_storage()
  130. {
  131.   allocate_itemsets();
  132.  
  133.   shiftset = NEW2(nsyms, short);
  134.   redset = NEW2(nrules + 1, short);
  135.   state_table = NEW2(STATE_TABLE_SIZE, core *);
  136. }
  137.  
  138.  
  139. void
  140. free_storage()
  141. {
  142.   FREE(shift_symbol);
  143.   FREE(redset);
  144.   FREE(shiftset);
  145.   FREE(kernel_base);
  146.   FREE(kernel_end);
  147.   FREE(kernel_items);
  148.   FREE(state_table);
  149. }
  150.  
  151.  
  152.  
  153. /* compute the nondeterministic finite state machine (see state.h for details)
  154. from the grammar.  */
  155. void
  156. generate_states()
  157. {
  158.   allocate_storage();
  159.   initialize_closure(nitems);
  160.   initialize_states();
  161.  
  162.   while (this_state)
  163.     {
  164.       /* Set up ruleset and itemset for the transitions out of this state.
  165.          ruleset gets a 1 bit for each rule that could reduce now.
  166.      itemset gets a vector of all the items that could be accepted next.  */
  167.       closure(this_state->items, this_state->nitems);
  168.       /* record the reductions allowed out of this state */
  169.       save_reductions();
  170.       /* find the itemsets of the states that shifts can reach */
  171.       new_itemsets();
  172.       /* find or create the core structures for those states */
  173.       append_states();
  174.  
  175.       /* create the shifts structures for the shifts to those states,
  176.          now that the state numbers transitioning to are known */
  177.       if (nshifts > 0)
  178.         save_shifts();
  179.  
  180.       /* states are queued when they are created; process them all */
  181.       this_state = this_state->next;
  182.     }
  183.  
  184.   /* discard various storage */
  185.   finalize_closure();
  186.   free_storage();
  187.  
  188.   /* set up initial and final states as parser wants them */
  189.   augment_automaton();
  190. }
  191.  
  192.  
  193.  
  194. /* Find which symbols can be shifted in the current state,
  195.    and for each one record which items would be active after that shift.
  196.    Uses the contents of itemset.
  197.    shift_symbol is set to a vector of the symbols that can be shifted.
  198.    For each symbol in the grammer, kernel_base[symbol] points to
  199.    a vector of item numbers activated if that symbol is shifted,
  200.    and kernel_end[symbol] points after the end of that vector.  */
  201. void
  202. new_itemsets()
  203. {
  204.   register int i;
  205.   register int shiftcount;
  206.   register short *isp;
  207.   register short *ksp;
  208.   register int symbol;
  209.  
  210. #ifdef    TRACE
  211.   fprintf(stderr, "Entering new_itemsets\n");
  212. #endif
  213.  
  214.   for (i = 0; i < nsyms; i++)
  215.     kernel_end[i] = NULL;
  216.  
  217.   shiftcount = 0;
  218.  
  219.   isp = itemset;
  220.  
  221.   while (isp < itemsetend)
  222.     {
  223.       i = *isp++;
  224.       symbol = ritem[i];
  225.       if (symbol > 0)
  226.     {
  227.           ksp = kernel_end[symbol];
  228.  
  229.           if (!ksp)
  230.         {
  231.           shift_symbol[shiftcount++] = symbol;
  232.           ksp = kernel_base[symbol];
  233.         }
  234.  
  235.           *ksp++ = i + 1;
  236.           kernel_end[symbol] = ksp;
  237.     }
  238.     }
  239.  
  240.   nshifts = shiftcount;
  241. }
  242.  
  243.  
  244.  
  245. /* Use the information computed by new_itemset to find the state numbers
  246.    reached by each shift transition from the current state.
  247.  
  248.    shiftset is set up as a vector of state numbers of those states.  */
  249. void
  250. append_states()
  251. {
  252.   register int i;
  253.   register int j;
  254.   register int symbol;
  255.  
  256. #ifdef    TRACE
  257.   fprintf(stderr, "Entering append_states\n");
  258. #endif
  259.  
  260.   /* first sort shift_symbol into increasing order */
  261.  
  262.   for (i = 1; i < nshifts; i++)
  263.     {
  264.       symbol = shift_symbol[i];
  265.       j = i;
  266.       while (j > 0 && shift_symbol[j - 1] > symbol)
  267.     {
  268.       shift_symbol[j] = shift_symbol[j - 1];
  269.       j--;
  270.     }
  271.       shift_symbol[j] = symbol;
  272.     }
  273.  
  274.   for (i = 0; i < nshifts; i++)
  275.     {
  276.       symbol = shift_symbol[i];
  277.       shiftset[i] = get_state(symbol);
  278.     }
  279. }
  280.  
  281.  
  282.  
  283. /* find the state number for the state we would get to
  284. (from the current state) by shifting symbol.
  285. Create a new state if no equivalent one exists already.
  286. Used by append_states  */
  287.  
  288. int
  289. get_state(symbol)
  290. int symbol;
  291. {
  292.   register int key;
  293.   register short *isp1;
  294.   register short *isp2;
  295.   register short *iend;
  296.   register core *sp;
  297.   register int found;
  298.  
  299.   int n;
  300.  
  301. #ifdef    TRACE
  302.   fprintf(stderr, "Entering get_state, symbol = %d\n", symbol);
  303. #endif
  304.  
  305.   isp1 = kernel_base[symbol];
  306.   iend = kernel_end[symbol];
  307.   n = iend - isp1;
  308.  
  309.   /* add up the target state's active item numbers to get a hash key */
  310.   key = 0;
  311.   while (isp1 < iend)
  312.     key += *isp1++;
  313.  
  314.   key = key % STATE_TABLE_SIZE;
  315.  
  316.   sp = state_table[key];
  317.  
  318.   if (sp)
  319.     {
  320.       found = 0;
  321.       while (!found)
  322.     {
  323.       if (sp->nitems == n)
  324.         {
  325.           found = 1;
  326.           isp1 = kernel_base[symbol];
  327.           isp2 = sp->items;
  328.  
  329.           while (found && isp1 < iend)
  330.         {
  331.           if (*isp1++ != *isp2++)
  332.             found = 0;
  333.         }
  334.         }
  335.  
  336.       if (!found)
  337.         {
  338.           if (sp->link)
  339.         {
  340.           sp = sp->link;
  341.         }
  342.           else   /* bucket exhausted and no match */
  343.         {
  344.           sp = sp->link = new_state(symbol);
  345.           found = 1;
  346.         }
  347.         }
  348.     }
  349.     }
  350.   else      /* bucket is empty */
  351.     {
  352.       state_table[key] = sp = new_state(symbol);
  353.     }
  354.  
  355.   return (sp->number);
  356. }
  357.  
  358.  
  359.  
  360. /* subroutine of get_state.  create a new state for those items, if necessary.  */
  361.  
  362. core *
  363. new_state(symbol)
  364. int symbol;
  365. {
  366.   register int n;
  367.   register core *p;
  368.   register short *isp1;
  369.   register short *isp2;
  370.   register short *iend;
  371.  
  372. #ifdef    TRACE
  373.   fprintf(stderr, "Entering new_state, symbol = %d\n", symbol);
  374. #endif
  375.  
  376.   if (nstates >= MAXSHORT)
  377.     toomany("states");
  378.  
  379.   isp1 = kernel_base[symbol];
  380.   iend = kernel_end[symbol];
  381.   n = iend - isp1;
  382.  
  383.   p = (core *) mallocate((unsigned) (sizeof(core) + (n - 1) * sizeof(short)));
  384.   p->accessing_symbol = symbol;
  385.   p->number = nstates;
  386.   p->nitems = n;
  387.  
  388.   isp2 = p->items;
  389.   while (isp1 < iend)
  390.     *isp2++ = *isp1++;
  391.  
  392.   last_state->next = p;
  393.   last_state = p;
  394.  
  395.   nstates++;
  396.  
  397.   return (p);
  398. }
  399.  
  400.  
  401. void
  402. initialize_states()
  403. {
  404.   register core *p;
  405. /*  register unsigned *rp1; JF unused */
  406. /*  register unsigned *rp2; JF unused */
  407. /*  register unsigned *rend; JF unused */
  408.  
  409.   p = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  410.   first_state = last_state = this_state = p;
  411.   nstates = 1;
  412. }
  413.  
  414.  
  415. void
  416. save_shifts()
  417. {
  418.   register shifts *p;
  419.   register short *sp1;
  420.   register short *sp2;
  421.   register short *send;
  422.  
  423.   p = (shifts *) mallocate((unsigned) (sizeof(shifts) +
  424.                        (nshifts - 1) * sizeof(short)));
  425.  
  426.   p->number = this_state->number;
  427.   p->nshifts = nshifts;
  428.  
  429.   sp1 = shiftset;
  430.   sp2 = p->shifts;
  431.   send = shiftset + nshifts;
  432.  
  433.   while (sp1 < send)
  434.     *sp2++ = *sp1++;
  435.  
  436.   if (last_shift)
  437.     {
  438.       last_shift->next = p;
  439.       last_shift = p;
  440.     }
  441.   else
  442.     {
  443.       first_shift = p;
  444.       last_shift = p;
  445.     }
  446. }
  447.  
  448.  
  449.  
  450. /* find which rules can be used for reduction transitions from the current state
  451.    and make a reductions structure for the state to record their rule numbers.  */
  452. void
  453. save_reductions()
  454. {
  455.   register short *isp;
  456.   register short *rp1;
  457.   register short *rp2;
  458.   register int item;
  459.   register int count;
  460.   register reductions *p;
  461.  
  462.   short *rend;
  463.  
  464.   /* find and count the active items that represent ends of rules */
  465.  
  466.   count = 0;
  467.   for (isp = itemset; isp < itemsetend; isp++)
  468.     {
  469.       item = ritem[*isp];
  470.       if (item < 0)
  471.     {
  472.       redset[count++] = -item;
  473.     }
  474.     }
  475.  
  476.   /* make a reductions structure and copy the data into it.  */
  477.  
  478.   if (count)
  479.     {
  480.       p = (reductions *) mallocate((unsigned) (sizeof(reductions) +
  481.                            (count - 1) * sizeof(short)));
  482.  
  483.       p->number = this_state->number;
  484.       p->nreds = count;
  485.  
  486.       rp1 = redset;
  487.       rp2 = p->rules;
  488.       rend = rp1 + count;
  489.  
  490.       while (rp1 < rend)
  491.     *rp2++ = *rp1++;
  492.  
  493.       if (last_reduction)
  494.     {
  495.       last_reduction->next = p;
  496.       last_reduction = p;
  497.     }
  498.       else
  499.     {
  500.       first_reduction = p;
  501.       last_reduction = p;
  502.     }
  503.     }
  504. }
  505.  
  506.  
  507.  
  508. /* Make sure that the initial state has a shift that accepts the
  509. grammar's start symbol and goes to the next-to-final state,
  510. which has a shift going to the final state, which has a shift
  511. to the termination state.
  512. Create such states and shifts if they don't happen to exist already.  */
  513. void
  514. augment_automaton()
  515. {
  516.   register int i;
  517.   register int k;
  518. /*  register int found; JF unused */
  519.   register core *statep;
  520.   register shifts *sp;
  521.   register shifts *sp2;
  522.   register shifts *sp1;
  523.  
  524.   sp = first_shift;
  525.  
  526.   if (sp)
  527.     {
  528.       if (sp->number == 0)
  529.     {
  530.       k = sp->nshifts;
  531.       statep = first_state->next;
  532.  
  533.       while (statep->accessing_symbol < start_symbol
  534.           && statep->number < k)
  535.         statep = statep->next;
  536.  
  537.       if (statep->accessing_symbol == start_symbol)
  538.         {
  539.           k = statep->number;
  540.  
  541.           while (sp->number < k)
  542.         {
  543.           sp1 = sp;
  544.           sp = sp->next;
  545.         }
  546.  
  547.           if (sp->number == k)
  548.         {
  549.           sp2 = (shifts *) mallocate((unsigned) (sizeof(shifts)
  550.                              + sp->nshifts * sizeof(short)));
  551.           sp2->next = sp->next;
  552.           sp2->number = k;
  553.           sp2->nshifts = sp->nshifts + 1;
  554.           sp2->shifts[0] = nstates;
  555.           for (i = sp->nshifts; i > 0; i--)
  556.             sp2->shifts[i] = sp->shifts[i - 1];
  557.  
  558.           sp1->next = sp2;
  559.           FREE(sp);
  560.         }
  561.           else
  562.         {
  563.           sp2 = NEW(shifts);
  564.           sp2->next = sp;
  565.           sp2->number = k;
  566.           sp2->nshifts = 1;
  567.           sp2->shifts[0] = nstates;
  568.  
  569.           sp1->next = sp2;
  570.           if (!sp)
  571.             last_shift = sp2;
  572.         }
  573.         }
  574.       else
  575.         {
  576.           k = statep->number;
  577.           sp = first_shift;
  578.  
  579.           sp2 = (shifts *) mallocate((unsigned) (sizeof(shifts)
  580.                              + sp->nshifts * sizeof(short)));
  581.           sp2->next = sp->next;
  582.           sp2->nshifts = sp->nshifts + 1;
  583.  
  584.           for (i = 0; i < k; i++)
  585.         sp2->shifts[i] = sp->shifts[i];
  586.  
  587.           sp2->shifts[k] = nstates;
  588.  
  589.           for (i = k; i < sp->nshifts; i++)
  590.         sp2->shifts[i + 1] = sp->shifts[i];
  591.  
  592.           first_shift = sp2;
  593.           if (last_shift == sp)
  594.         last_shift = sp2;
  595.  
  596.           FREE(sp);
  597.  
  598.           insert_start_shift();
  599.         }
  600.     }
  601.       else
  602.     {
  603.       sp = NEW(shifts);
  604.       sp->next = first_shift;
  605.       sp->nshifts = 1;
  606.       sp->shifts[0] = nstates;
  607.  
  608.       first_shift = sp;
  609.  
  610.       insert_start_shift();
  611.     }
  612.     }
  613.   else
  614.     {
  615.       sp = NEW(shifts);
  616.       sp->nshifts = 1;
  617.       sp->shifts[0] = nstates;
  618.  
  619.       first_shift = sp;
  620.       last_shift = sp;
  621.  
  622.       insert_start_shift();
  623.     }
  624.  
  625.   statep = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  626.   statep->number = nstates;
  627.   last_state->next = statep;
  628.   last_state = statep;
  629.  
  630.   sp = NEW(shifts);
  631.   sp->number = nstates++;
  632.   sp->nshifts = 1;
  633.   sp->shifts[0] = nstates;
  634.   last_shift->next = sp;
  635.   last_shift = sp;
  636.  
  637.   final_state = nstates;
  638.  
  639.   statep = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  640.   statep->number = nstates++;
  641.   last_state->next = statep;
  642.   last_state = statep;
  643. }
  644.  
  645.  
  646. /* subroutine of augment_automaton */
  647. void
  648. insert_start_shift()
  649. {
  650.   register core *statep;
  651.   register shifts *sp;
  652.  
  653.   statep = (core *) mallocate((unsigned) (sizeof(core) - sizeof(short)));
  654.   statep->number = nstates;
  655.   statep->accessing_symbol = start_symbol;
  656.  
  657.   last_state->next = statep;
  658.   last_state = statep;
  659.  
  660.   sp = NEW(shifts);
  661.   sp->number = nstates++;
  662.   sp->nshifts = 1;
  663.   sp->shifts[0] = nstates;
  664.  
  665.   last_shift->next = sp;
  666.   last_shift = sp;
  667. }
  668.